﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Collections;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace zuoye10
{
    public class Searcher
    {
        private string url = "";
        private int count = 0;
        public Form1 r = null;
        public delegate void event_handler(string sender);
        public event event_handler tmp;

        /*async 异步函数*/
        public async static void Main(string startUrl, Form1 r = null)
        {
            Searcher myCrawler = new Searcher();
            startUrl = "http://www.baidu.com/s?wd=" + startUrl.Replace(" ", "%20");
            myCrawler.r = r;
            myCrawler.tmp += myCrawler.r.update_Field;
            myCrawler.url = startUrl;
            Task n = new Task(myCrawler.Crawl);
            n.Start();
            await n;
        }
        private void Crawl()
        {
            if (r != null) updateField("开始获取结果\n");
            string html = "";
            for (int i = 1; i < 9; i++)
            {
                Thread n = new Thread(() => html = DownLoad(url + "&pn=" + (10 * i).ToString()));
                n.Start();
                Thread n2 = new Thread(() => Parse(html));
                n.Join();
                n2.Start();
            }
            if (r != null) updateField("查询结束\n");

        }

        public string DownLoad(string url)
        {
            try
            {
                WebClient webClient = new WebClient();
                webClient.Encoding = Encoding.UTF8;
                webClient.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36");
                webClient.Headers.Add(HttpRequestHeader.Cookie, "BIDUPSID=F70728F3F06AC25634EFD41D3ABCE51F; PSTM=1637581328; BD_UPN=12314753; BDSFRCVID_BFESS=ClPOJeCmHx76oAoHdXdRJ7xXCgKK0gOTHllnsooNXlOe_8tVJeC6EG0Ptf8g0KubUElrogKK0gOTH6KF_2uxOjjg8UtVJeC6EG0Ptf8g0M5; H_BDCLCKID_SF_BFESS=tJ-qoII5JIL3fP36qRbobR3DbMnf2D6X56PsaRbxBhcqEn6S25t-hU_eyMOvKPvZJ5bv5b3cWKJJ8UbSh-v_54DTbxIjb5QP-tjpaJ5nJq5nhMJmb67JDMP0-Ro42njy523ion3vQpP-OpQ3DRoWXPIqbN7P-p5Z5mAqKl0MLPbtbb0xXj_0D6vBDauftTKsb5vfstOO5t-Veb76hCT_bJ3MLUvTWMT-0Kj7XI0KahjoObbybh5kjnkRLUJiB5OMBan7WnR8-b7o8xn1XPoreb0ZyxomtfQxtNRJ0DnjtpvhHx5Hhb5obUPUDMJ9LUkqW2cdot5yBbc8eIna5hjkbfJBQttjQn3hfIkj2CKLK-oj-DLxjT0b3e; BDUSS=1FROGp1SG0wWm9aS1VkTn5xeXFqaDFHdVY5NUdLd0VmTW9FaTUxfkFUflUtQWhpRVFBQUFBJCQAAAAAAAAAAAEAAABianMueHnQobbTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANRr4WHUa-Fhb; BDUSS_BFESS=1FROGp1SG0wWm9aS1VkTn5xeXFqaDFHdVY5NUdLd0VmTW9FaTUxfkFUflUtQWhpRVFBQUFBJCQAAAAAAAAAAAEAAABianMueHnQobbTAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANRr4WHUa-Fhb; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BAIDUID=F70728F3F06AC256B5594C86C9B29266:SL=0:NR=50:FG=1; H_PS_PSSID=35839_31660_36166_34584_36122_36338_35978_36126_36232_26350_36313_36061; delPer=0; BD_CK_SAM=1; PSINO=6; BA_HECTOR=250lalak8k800l807l1h6444d0r; B64_BOT=1; sug=3; sugstore=0; ORIGIN=0; bdime=0; H_PS_645EC=928eT0PTBbAiIlmeyJium2agGYR%2FmVLiDEVXrI2j77BAmaT0KZC%2FJRHMOmI");
                string html = webClient.DownloadString(url);
                return html;
            }
            catch (Exception ex)
            {
                updateField("爬取" + url + "错误：");
                updateField(ex.Message + "\n");
                //throw;
                return "";
            }
        }
        public void Parse(string html)
        {
            List<string> result = new List<string>();
            MatchCollection result_group = Regex.Matches(html, "{\"title\":\"(.*?)\",\"url\":\"(.*?)\"}");
            foreach (Match i in result_group)
            {
                string m = i.Value;
                string half_title = Regex.Replace(m.Split(',')[0].Substring(10), "<em>|</em>", "").TrimEnd('"');
                result.Add(half_title);
            }
            foreach (string j in result)
            {
                if (count <= 200)
                {
                    r.update_Field(j + "\n");
                    count += j.Length;
                }
            }

        }


        public void updateField(string s)
        {
            tmp(s);
        }
    }
}
